package com.xbongbong.util;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.domain.entity.UpdateDataValueEntity;
import com.xbongbong.pro.domain.entity.UpdateSubDataEntity;
import com.xbongbong.pro.enums.DataTypeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2020/10/10 14:02
 */
public class DataTypeUtil {

    /**
     * 设置数据类型
     * @param updateDataValueEntity
     * @param value
     * @param fieldType
     * @return java.lang.Object
     * @author 吴峰
     * @date 2020/9/29 14:50
     * @throws XbbException
     */
    public static Object setDataType(UpdateDataValueEntity updateDataValueEntity, Object value, Integer fieldType) {
        boolean isArray = FieldTypeEnum.isArray(fieldType);
        boolean isList = value instanceof List || isArray;
        if (isList) {
            updateDataValueEntity.setDataType(DataTypeEnum.JSON_ARRAY.getDataType());
            if (value == null) {
                value = new ArrayList<>();
            }
        } else if (value instanceof Map || FieldTypeEnum.isJsonObject(fieldType)) {
            updateDataValueEntity.setDataType(DataTypeEnum.JSON_OBJECT.getDataType());
        }

        return value;
    }

    /**
     * 将子表单需要更新的数据放入主表数据中
     * @param updateDataList
     * @param updateSubDataList
     * @param parentAttr
     * @return void
     * @author 吴峰
     * @date 2020/10/10 15:51
     * @throws XbbException
     */
    public static void setSubDataList(List<UpdateDataEntity> updateDataList, List<UpdateSubDataEntity> updateSubDataList, String parentAttr, String corpid) {
        if (CollectionsUtil.isEmpty(updateSubDataList)) {
            return;
        }
        Map<Long, List<UpdateSubDataEntity>> updateSubDataMap = new HashMap<>(updateSubDataList.size());
        updateSubDataList.forEach(item->{
            if (updateSubDataMap.containsKey(item.getParentId())) {
                List<UpdateSubDataEntity> list = updateSubDataMap.get(item.getParentId());
                item.setAttr(parentAttr);
                list.add(item);
                updateSubDataMap.put(item.getParentId(), list);
            } else {
                List<UpdateSubDataEntity> list = new ArrayList<>();
                item.setAttr(parentAttr);
                list.add(item);
                updateSubDataMap.put(item.getParentId(), list);
            }
        });
        // 将子表单放入主表中，主要是为了拼装数据
        if (CollectionsUtil.isNotEmpty(updateDataList)) {
            updateDataList.forEach(item->{
                List<UpdateDataValueEntity> list = item.getData();
                UpdateDataValueEntity updateDataValueEntity = new UpdateDataValueEntity(parentAttr, parentAttr, null, DataTypeEnum.JSON_ARRAY.getDataType(), updateSubDataMap.get(item.getId()));
                list.add(updateDataValueEntity);
            });
        } else if (!updateSubDataMap.isEmpty()){
            updateSubDataMap.forEach((key, value)->{
                List<UpdateDataValueEntity> data = new ArrayList<>();
                UpdateDataValueEntity updateDataValueEntity = new UpdateDataValueEntity(parentAttr, parentAttr, null, DataTypeEnum.JSON_ARRAY.getDataType(), value);
                data.add(updateDataValueEntity);
                UpdateDataEntity updateDataEntity = new UpdateDataEntity(key, corpid,  null, data, DateTimeUtil.getInt());
                updateDataList.add(updateDataEntity);
            });
        }
    }
}
